package com.spider.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.log4j.Logger;

public class HTTPUtils {
	private static Logger logger = Logger.getLogger(HTTPUtils.class);

	public static String getResponseByURL(String urlparam) {
		try {
			// 传入网址
			URL url = new URL(urlparam);
			// 获得网页内容
			InputStream is = url.openStream();
			// 将InputStream转换成Reader，并使用缓冲读取，提高效率，同时可以按行读取内容
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
			String line = null;
			StringBuffer content = new StringBuffer();
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
			return content.toString();
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
			return null;
		}
	}

	/**
	 * 根据传入的URL地址得到整个网页的内容
	 * 
	 * @param urlparam
	 *            获取网页内容的地址
	 */
	public static void getHTMLByURL(String urlparam) {
		try {
			// 传入网址
			URL url = new URL(urlparam);
			// 获得网页内容
			InputStream is = url.openStream();
			// 将InputStream转换成Reader，并使用缓冲读取，提高效率，同时可以按行读取内容
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			StringBuffer content = new StringBuffer();
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			logger.info(content.toString());
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}
	}

	/**
	 * 通过设置代理读取指定URL地址的网页内容
	 * 
	 * @param urlparam
	 * @param ip
	 * @param port
	 */
	public static void getHTMLByURL(String urlparam, String ip, int port) {
		try {
			URL url = new URL(urlparam);
			// 首先创建HTTP代理，指定代理的地址和端口
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
			/*
			 * 首先打开一个连接对象，可以通过这个对象， 在真正发起请求之前设置一些其它的信息比如：代理服务器等
			 */
			URLConnection conn = url.openConnection(proxy);
			InputStream is = conn.getInputStream();
			// 将InputStream转换为Reader,并使用缓冲读取提高效率，同时按行读取内容
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			StringBuffer content = new StringBuffer();
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
			logger.info(content.toString());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 通过设置代理读取指定URL地址的网页内容
	 * 
	 * @param urlparam
	 * @param ip
	 * @param port
	 */
	public static String retHTMLByURL(String urlparam, String ip, int port) {
		StringBuffer content = new StringBuffer();
		try {
			URL url = new URL(urlparam);
			// 首先创建HTTP代理，指定代理的地址和端口
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(ip, port));
			/*
			 * 首先打开一个连接对象，可以通过这个对象， 在真正发起请求之前设置一些其它的信息比如：代理服务器等
			 */
			URLConnection conn = url.openConnection(proxy);
			InputStream is = conn.getInputStream();
			// 将InputStream转换为Reader,并使用缓冲读取提高效率，同时按行读取内容
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content.toString();
	}

	/**
	 * 通过设置代理读取指定URL地址的网页内容
	 * 
	 * @param urlparam
	 * @param ip
	 * @param port
	 */
	public static String retHTMLByProxy(String urlparam) {
		String content = "";
		try {
			content = retHTML(urlparam, "172.16.251.58", 3128).toString();
		} catch (Exception ex) {
			logger.info("获取余票信息发生异常!" + ex.getMessage());
		}
		return content;
	}

	/**
	 * 通过设置代理读取指定URL地址的网页内容
	 * 
	 * @param urlparam
	 * @param ip
	 * @param port
	 */
	public static String retHTMLByURL(String urlparam) {
		StringBuffer content = new StringBuffer();
		try {
			URL url = new URL(urlparam);
			/*
			 * 首先打开一个连接对象，可以通过这个对象， 在真正发起请求之前设置一些其它的信息比如：代理服务器等
			 */
			URLConnection conn = url.openConnection();
			InputStream is = conn.getInputStream();
			// 将InputStream转换为Reader,并使用缓冲读取提高效率，同时按行读取内容
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content.toString();
	}

	public static String retHTML(String url, String ip, int port) throws ClientProtocolException, IOException {
		// HttpClient主要负责执行请求
		HttpClient httpclient = new DefaultHttpClient();
		// 首先创建HTTP代理，指定代理的地址和端口
		HttpHost httpHost = new HttpHost(ip, port);
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
		// 利用HTTP GET向服务器发起请求
		HttpGet get = new HttpGet(url);
		// 获得服务器响应的所有信息
		HttpResponse response = httpclient.execute(get);
		// 获得服务器响应回来的消息体(不包括HTTP HEAD)
		HttpEntity entity = response.getEntity();
		StringBuffer content = new StringBuffer();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
		}
		// 释放所有的链接，一般在所有的请求处理完成之后，才需要释放
		httpclient.getConnectionManager().shutdown();
		return content.toString();
	}

	/**
	 * 通过Get方式请求指定url获取返回值
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String retHTMLByGet(String url) throws ClientProtocolException, IOException {
		// HttpClient主要负责执行请求
		HttpClient httpclient = new DefaultHttpClient();
		// 利用HTTP GET向服务器发起请求
		HttpGet get = new HttpGet(url);
		// 获得服务器响应的所有信息
		HttpResponse response = httpclient.execute(get);
		// 获得服务器响应回来的消息体(不包括HTTP HEAD)
		HttpEntity entity = response.getEntity();
		StringBuffer content = new StringBuffer();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
		}
		// 释放所有的链接，一般在所有的请求处理完成之后，才需要释放
		httpclient.getConnectionManager().shutdown();
		return content.toString();
	}

	/**
	 * 通过Post方式请求指定url获取返回值
	 * 
	 * @param url
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String retHTMLByPost(String url, Map<String, String> param) throws ClientProtocolException, IOException {
		// HttpClient主要负责执行请求
		HttpClient httpclient = new DefaultHttpClient();
		// 利用HTTP POST向服务器发起请求
		HttpPost post = new HttpPost(url);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		// 添加参数
		for (Entry<String, String> entry : param.entrySet()) {
			params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		}
		post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
		// 获得服务器响应的所有信息
		HttpResponse response = new DefaultHttpClient().execute(post);
		// 获得服务器响应回来的消息体(不包括HTTP HEAD)
		HttpEntity entity = response.getEntity();
		StringBuffer content = new StringBuffer();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
		}
		// 释放所有的链接，一般在所有的请求处理完成之后，才需要释放
		httpclient.getConnectionManager().shutdown();
		return content.toString();
	}

	public static void getHTML(String url, String ip, int port) throws ClientProtocolException, IOException {
		// HttpClient主要负责执行请求
		HttpClient httpclient = new DefaultHttpClient();
		// 首先创建HTTP代理，指定代理的地址和端口
		HttpHost httpHost = new HttpHost(ip, port);
		httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, httpHost);
		// 利用HTTP GET向服务器发起请求
		HttpGet get = new HttpGet(url);
		// 获得服务器响应的所有信息
		HttpResponse response = httpclient.execute(get);
		// 获得服务器响应回来的消息体(不包括HTTP HEAD)
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			StringBuffer content = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
			System.err.println(content.toString());
		}
		// 释放所有的链接，一般在所有的请求处理完成之后，才需要释放
		httpclient.getConnectionManager().shutdown();
	}

	public static void getHTML(String url) throws ClientProtocolException, IOException {
		// HttpClient主要负责执行请求
		DefaultHttpClient httpclient = new DefaultHttpClient();
		// 利用HTTP GET向服务器发起请求
		HttpGet get = new HttpGet(url);
		// 获得服务器响应的所有信息
		HttpResponse response = httpclient.execute(get);
		// 获得服务器响应回来的消息体(不包括HTTP HEAD)
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			StringBuffer content = new StringBuffer();
			String line = null;
			while ((line = br.readLine()) != null) {
				content.append(line);
			}
			is.close();
			System.err.println(content.toString());
		}
		// 释放所有的链接，一般在所有的请求处理完成之后，才需要释放
		httpclient.getConnectionManager().shutdown();
	}
}
